import {
  getFeedback,
  submitFeedback
} from "../../apis/user";
import {
  checkUpload,
  uploadOSS,
  globalData
} from '../../utils/request';
import {
  getOSSAutograph
} from '../../apis/card';
const app = getApp();

Page({
  data: {
    tagList: [],
    tagIndex: '',
    textValue: '',
    inputValue: '',
    imgList: [],
    uploadList: [],
    popupShow: false,
    OSSAutograph: {}
  },

  onLoad() {
    this.getTag();
    this.getOSSAutograph();
  },

  // 获取分类标签
  getTag() {
    getFeedback().then(res => {
      if (res.data.code === 200) {
        this.setData({
          tagList: res.data.rows
        });
      };
    });
  },
  // 选择标签
  selectTag(e) {
    let {
      index
    } = e.currentTarget.dataset;
    this.setData({
      tagIndex: index
    })
  },
  // 输入
  textInput(e) {
    let {
      value
    } = e.detail;
    this.setData({
      textValue: value
    });
  },
  phoneInput(e) {
    let {
      value
    } = e.detail;
    this.setData({
      inputValue: value
    });
  },
  // 弹出
  showPopup() {
    this.setData({
      popupShow: true
    });
  },
  // 隐藏
  hidePopup() {
    this.setData({
      popupShow: false
    });
  },
  // 微信上传图片
  localImg(e) {
    let {
      type
    } = e.currentTarget.dataset;
    let num = 8 - this.data.imgList;
    wx.chooseImage({
      count: num,
      sourceType: [type],
      sizeType: ['compressed'],
      success: res => {
        res.tempFilePaths.forEach(v => {
          this.data.imgList.push(v);
        });
        this.setData({
          imgList: this.data.imgList,
          popupShow: false
        });
      }
    })
  },

  // OSS签名
  getOSSAutograph() {
    getOSSAutograph().then(res => {
      if (res.data.code === 200) {
        this.setData({
          OSSAutograph: res.data.data
        });
      };
    });
  },

  // 校验
  imagesCheck(arr, i) {
    wx.showLoading({
      title: '校验中...'
    });
    if (i === arr.length) {
      this.imageUploadOSS(arr, 0);
    } else {
      checkUpload(arr[i]).then(res => {
        let {
          code
        } = JSON.parse(res.data);
        if (code === 200) {
          i++;
          this.imagesCheck(arr, i);
        } else {
          wx.hideLoading({});
          wx.showToast({
            title: `第${i+1}张图片违规，请重新上传`,
            icon: 'none',
            duration: 2000
          });
        };
      });
    };
  },
  // 上传OSS
  imageUploadOSS(arr, i) {
    wx.showLoading({
      title: '上传中...'
    });
    if (i === arr.length) {
      wx.hideLoading({});
      this.submitRequest();
    } else {
      let fileName = globalData.userId + '-' + new Date().getTime() + '.png';
      uploadOSS(arr[i], this.data.OSSAutograph.host, {
        key: `${this.data.OSSAutograph.dir}/${fileName}`,
        policy: this.data.OSSAutograph.policy,
        OSSAccessKeyId: this.data.OSSAutograph.accessid,
        signature: this.data.OSSAutograph.signature,
        success_action_status: '200'
      }).then(res => {
        if (res.statusCode === 200) {
          this.setData({
            [`uploadList[${i}]`]: `${this.data.OSSAutograph.host}/${this.data.OSSAutograph.dir}/${fileName}`
          });
          i++;
          this.imageUploadOSS(arr, i);
        };
      });
    };
  },

  // 提交
  submit() {
    if (this.data.tagIndex === '') {
      wx.showToast({
        title: '请选择标签',
        icon: 'none',
        duration: 1000
      });
      return
    };
    if (this.data.textValue === '') {
      wx.showToast({
        title: '请输入内容',
        icon: 'none',
        duration: 1000
      });
      return
    };
    if (this.data.inputValue === '') {
      wx.showToast({
        title: '请输入联系方式',
        icon: 'none',
        duration: 1000
      });
      return
    } else if (this.data.inputValue.length !== 11) {
      wx.showToast({
        title: '请输入11位手机号',
        icon: 'none',
        duration: 1000
      });
      return
    };

    if (this.data.imgList.length === 0) {
      this.submitRequest();
    } else {
      this.imagesCheck(this.data.imgList, 0);
    };
  },
  // 提交请求
  submitRequest() {
    submitFeedback(this.data.tagList[this.data.tagIndex].title, this.data.textValue, this.data.uploadList.join(','), this.data.inputValue).then(res => {
      if (res.data.code === 200) {
        wx.showToast({
          title: '反馈成功',
          icon: 'success',
          duration: 1000,
          success: res => {
            let timer = setTimeout(() => {
              wx.navigateBack({
                delta: 1
              });
              clearTimeout(timer);
            }, 1000);
          }
        });
      };
    });
  },

  emptyFunction() {}
})